import { ref } from 'vue'
import { adapter, request } from '@/request'
import { promotionCreativeByUpdate } from '@/core/api/promotion'
import type { Mode } from '@/core/interfaces/common'
import type { Creative } from '@/core/interfaces/promotion'
import type { PlatformEnum } from '@/core/enums/platform'
import type { RequestWrapper } from '@/core/interfaces/common'

export interface Goods {
  id?: number
  orderWordId: number
  name: string
  pictureName: string
  pictureURL: string
  imageContent: string
  info: string
  imageId?: string
  creativeGroupId?: number
  creativeGroupName?: string
  clientType?: PlatformEnum
  selectLists?: any
  deviceName?: string
  service?: string
  feature?: string
  details?: string
  baiduPictureURL?: string
}

export interface PromotionCreativeData {
  id?: number
  serviceUserName: string
  campaignId: number
  orderId: number
  orderWordId: number
  word: string
  destinationUrl: string
  creativeId: number
  title: string
  clientType: PlatformEnum
  groupId: Creative['groupId']
  content?: string
  description1: string
  description2: string
  files: File[]
  pcGoods: Goods[]
  wapGoods: Goods[]
  selectLists?: any
}

export default function updatePromotionCreative() {
  const service = adapter<RequestWrapper<any>>(request)
  const loading = ref(false)
  const response = ref<RequestWrapper<any>>()

  const fetch = (data: Partial<PromotionCreativeData>, mode: Mode) => {
    const { url, method } = promotionCreativeByUpdate(mode)
    loading.value = true

    return service({
      config: {
        url,
        method,
        data
      },
      callback: (res) => {
        response.value = res.data.data
      },
      done: () => {
        loading.value = false
      }
    })
  }

  return [response, fetch, loading] as const
}
